// index.js
// 获取应用实例
const app = getApp()
const config = app.globalData;

Page({
    data: {
        globalData: {},
        equipmentNo: '',
        equipmentType: '',

        showConfirm: false,
        showConfirmMsg: '',
        confirmFunc: null,
    },
    bindDevice(){

        if(this.data.equipmentNo){
            wx.navigateTo({
                url: '../../pages/wristDetail/index',
            });
            return ;
        }

        this.setData({
            showConfirm: true,
            showConfirmMsg: "暂时没有设备，需要去添加吗？",
            confirmFunc: function() {
                if(config.userInfo.phone == null){
                        this.goLogin()
                        this.setData({
                            isLogin:false
                        })
                } else {
                    wx.navigateTo({
                        url: '../../pages/updateEquipment/index?equipment=true',
                    })
                }
            }
        })
    },

    // 跳转到我的页面
    switchTabMy(){
        wx.switchTab({
            url: '../../pages/my/index'
        })
    },
    // 去登录页面
    goLogin(){
        wx.navigateTo({
            url: '../../pages/login/index',
        })
    },

    onLoad() {
        this.setData({
            globalData: config,
            equipmentNo: config.userInfo.equipmentNo,
            equipmentType: config.userInfo.equipmentType
        })
    },
    onShow(){
        if(config.userInfo.phone == null){
            this.goLogin()
        }else{
            this.getEquipmentInfo()
        }
    },
    // 获取设备信息
    getEquipmentInfo(){
        let that = this;
        config.userInfo.equipmentNo = null;
        config.userInfo.equipmentType = null;
        that.setData({
            equipmentNo: null,
            equipmentType: null,
        });
        wx.request({
            url: config.url + 'api/equipment/get?phone=' + config.userInfo.phone + '&equipmentType=' + config.systemEquipmentType,
            success(res){
                if(res.data.code === 0) {
                    let info = res.data.data;
                    if (info) {
                        config.userInfo.equipmentNo = info.equipmentNo;
                        config.userInfo.equipmentType = info.equipmentType;
                        that.setData({
                            equipmentNo: info.equipmentNo,
                            equipmentType: info.equipmentType,
                        });
                    }
                }
            }
        })
    },
})
